{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "04aaf32a",
   "metadata": {},
   "source": [
    "---\n",
    "* 阶段：week12\n",
    "* 时间：2023/11/22\n",
    "---"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c168bf13",
   "metadata": {},
   "source": [
    "#  总结"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e13df2b1",
   "metadata": {},
   "source": [
    "## 数据获取\n",
    "* 模块：request-html\n",
    "> 1.静态页面的数据获取\n",
    ">> 1. 检索静态页面element的标签（class/name/id）\n",
    ">> 2. 通过标签获取text、url、src、href\n",
    ">\n",
    "> 2.动态页面的数据获取\n",
    ">> 1. F12 处理ajex，获取目标数据（库）的url\n",
    ">> 2. 通过ajex对应的目标url获取到json数据"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "25f30682",
   "metadata": {},
   "source": [
    "## 数据分析\n",
    "> 1. 数据整合，文本数据 --> dateframe\n",
    "> 2. 数据清洗 --> 筛选有价值的数据项（目标）\n",
    "> 3. 数据重塑 --> 整合成较为直观的分门别类的数据\n",
    "> 4. 数据输出 --> 模块化、格式化输出（数据需求）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01696b71",
   "metadata": {},
   "source": [
    "## 数据可视化（交互式）\n",
    "> 1. 可视化图表输出\n",
    "> 2. 交互式数据可视化应用（价值）"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "109773b2",
   "metadata": {},
   "source": [
    "## 专业特色课程（网络舆情与研判）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6b95fbba",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "12fb3fbc",
   "metadata": {},
   "source": [
    "# Flask数据可视化详解"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "1c7d7d88",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: pycharts in c:\\users\\asus\\anaconda3\\lib\\site-packages (0.1.5)\n"
     ]
    }
   ],
   "source": [
    "!pip install pycharts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "d9d28e1d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " * Serving Flask app '__main__'\n",
      " * Debug mode: off\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.\n",
      " * Running on http://127.0.0.1:5000\n",
      "Press CTRL+C to quit\n",
      "127.0.0.1 - - [29/Nov/2023 19:51:53] \"GET / HTTP/1.1\" 200 -\n",
      "127.0.0.1 - - [29/Nov/2023 19:51:55] \"GET /favicon.ico HTTP/1.1\" 404 -\n"
     ]
    }
   ],
   "source": [
    "# python后端与前端连接的一个框架\n",
    "from flask import Flask\n",
    "# Jinja2 前端页面展示的框架\n",
    "from jinja2 import Environment, FileSystemLoader\n",
    "from markupsafe import Markup  # 添加这一行\n",
    "# pyecharts：A Python Echarts Plotting Library\n",
    "from pyecharts.globals import CurrentConfig\n",
    "\n",
    "\n",
    "CurrentConfig.GLOBAL_ENV = Environment(loader=FileSystemLoader(\"./templates\"))\n",
    "# pyecharts 模板 需要在github下载templates的模板文件\n",
    "\n",
    "from pyecharts import options as opts\n",
    "# options 图表的配置项 graph\n",
    "\n",
    "from pyecharts.charts import Bar\n",
    "# Bar 柱状图 pyecharts 封装好的图形，可参见 gallery 示例\n",
    "\n",
    "from pyecharts.charts import Geo\n",
    "from pyecharts.globals import ChartType, SymbolType\n",
    "\n",
    "app = Flask(__name__, static_folder=\"templates\")\n",
    "\n",
    "\n",
    "# 1. 找到合适的可视化图表，找到gallery示例代码，测试一下示例代码，\n",
    "# 2. 准备数据分析结果的参数，替换示例代码的参数\n",
    "# 3. 创建制作图的函数\n",
    "def bar_base() -> Bar:\n",
    "    c = (\n",
    "        Bar()\n",
    "        .add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"])\n",
    "        .add_yaxis(\"商家A\", [5, 20, 36, 10, 75, 90])\n",
    "        .add_yaxis(\"商家B\", [15, 25, 16, 55, 48, 8])\n",
    "        .set_global_opts(title_opts=opts.TitleOpts(title=\"Bar-基本示例\", subtitle=\"我是副标题\"))\n",
    "    )\n",
    "    return c\n",
    "\n",
    "def geo_lines():\n",
    "    c = (\n",
    "        Geo()\n",
    "        .add_schema(maptype=\"china\")\n",
    "        .add(\n",
    "            \"\",\n",
    "            [(\"广州\", 55), (\"北京\", 66), (\"杭州\", 77), (\"重庆\", 88)],\n",
    "            type_=ChartType.EFFECT_SCATTER,\n",
    "            color=\"white\",\n",
    "        )\n",
    "        .add(\n",
    "            \"geo\",\n",
    "            [(\"广州\", \"上海\"), (\"广州\", \"北京\"), (\"广州\", \"杭州\"), (\"广州\", \"重庆\")],\n",
    "            type_=ChartType.LINES,\n",
    "            effect_opts=opts.EffectOpts(\n",
    "                symbol=SymbolType.ARROW, symbol_size=6, color=\"blue\"\n",
    "            ),\n",
    "            linestyle_opts=opts.LineStyleOpts(curve=0.2),\n",
    "        )\n",
    "        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))\n",
    "        .set_global_opts(title_opts=opts.TitleOpts(title=\"Geo-Lines\"))\n",
    "    )\n",
    "    return c\n",
    "\n",
    "# 4. 将可视化图形通过markup方法，绘制在HTML的页面上\n",
    "@app.route(\"/\")\n",
    "def index():\n",
    "    c = geo_lines()\n",
    "    return Markup(c.render_embed())\n",
    "\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd72e174",
   "metadata": {},
   "source": [
    "# Flask前后端分离"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "5264dec5",
   "metadata": {},
   "outputs": [],
   "source": [
    "from random import randrange\n",
    "from flask import Flask, render_template\n",
    "from pyecharts import options as opts\n",
    "from pyecharts.charts import Bar\n",
    "from pyecharts.faker import Faker\n",
    "from pyecharts.globals import ChartType\n",
    "\n",
    "app = Flask(__name__, static_folder=\"templates\")\n",
    "\n",
    "\n",
    "def bar_base() -> Bar:\n",
    "    c = (\n",
    "        Bar()\n",
    "        .add_xaxis([\"衬衫\", \"羊毛衫\", \"雪纺衫\", \"裤子\", \"高跟鞋\", \"袜子\"])\n",
    "        .add_yaxis(\"商家A\", [randrange(0, 100) for _ in range(6)])\n",
    "        .add_yaxis(\"商家B\", [randrange(0, 100) for _ in range(6)])\n",
    "        .set_global_opts(title_opts=opts.TitleOpts(title=\"Bar-基本示例\", subtitle=\"我是副标题\"))\n",
    "    )\n",
    "    return c\n",
    "\n",
    "def geo_lines():\n",
    "    c = (\n",
    "        Geo()\n",
    "        .add_schema(maptype=\"广东\")\n",
    "        .add(\n",
    "            \"geo\",\n",
    "            [list(z) for z in zip(Faker.guangdong_city, Faker.values())],\n",
    "            type_=ChartType.HEATMAP,\n",
    "        )\n",
    "        .set_series_opts(label_opts=opts.LabelOpts(is_show=False))\n",
    "        .set_global_opts(\n",
    "            visualmap_opts=opts.VisualMapOpts(), title_opts=opts.TitleOpts(title=\"Geo-广东地图\")\n",
    "        )\n",
    "    )\n",
    "    return c\n",
    "\n",
    "@app.route(\"/\")\n",
    "def index():\n",
    "    return render_template(\"index.html\")\n",
    "\n",
    "\n",
    "@app.route(\"/barChart\")\n",
    "def get_bar_chart():\n",
    "    c = bar_base()\n",
    "    return c.dump_options_with_quotes()\n",
    "\n",
    "@app.route(\"/geo_lines\")\n",
    "def get_geo_lines():\n",
    "    c = geo_lines()\n",
    "    return c.dump_options_with_quotes()\n",
    "\n",
    "if __name__ == \"__main__\":\n",
    "    app.run()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "88e4eb80",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
